#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include "Team.h"
#include "Player.h"

Team::Team()
{
currentPoints=0;

m_prevTime=Time(1,0,0);
m_posTime=Time(0,0.0,0.0);


}


Team::Team(string TeamName)
{
m_name=TeamName;
currentPoints=0;
m_prevTime=Time(1,0,0.0);
m_posTime=Time(0,0.0,0.0);


}

void Team::setName(string TeamName)
{
m_name=TeamName;
}

string Team::getName()
{
return m_name;
}

void Team::setPossess(bool hasPossess)
{
m_hasBall=hasPossess;
}

bool Team::getPossess()
{
return m_hasBall;
}
//Adds points to player and team
void Team::incPts(int points,int number)
{

//increments the member variable for the team
currentPoints=currentPoints+points;

//inrements the individual players points
addPlayer(number);

for (int i=0 ; i < players.size(); i++ )
   {
		
	if((players[i].getPlayerNumber())==number)
        {
	players[i].incPlayerPoints(points);
	}
   }

}


void Team::displayPointsByPlayer()
{


for (int i=0; i < players.size(); i++)
	{
	cout<<m_name<<" Player No:" <<players[i].getPlayerNumber()<<" Points: " << players[i].getPlayerPoints()<<endl;
	}	
}	


int Team::getPoints()
{
return currentPoints;
}


void Team::addPlayer(int jerseyNum)
{
//loops trough current players
//adds this player if it hasn't been added yet
Player newPlayer(jerseyNum),currentPlayer;
bool alreadyAdded=false;
vector<Player>::iterator it;
  for ( it=players.begin() ; it < players.end(); it++ )
   {
	currentPlayer=*it;
	if (currentPlayer.getPlayerNumber()==jerseyNum)
	alreadyAdded=true;
   }
 if (!alreadyAdded)	
 players.push_back(newPlayer);

}

void Team::addPlayer(int jerseyNum, string name)
{
Player newPlayer(jerseyNum,name);
players.push_back(newPlayer);
}

Player Team::getPlayer(int jerseyNum)
{
vector<Player>::iterator it;
Player currentPlayer;
	for (it=players.begin() ; it < players.end(); it++)
	{
	currentPlayer=*it;
		if (currentPlayer.getPlayerNumber()==jerseyNum)
		{
		return currentPlayer;
		}
	}

}

void Team::addPoints(Shot *ptrToShot) 
{

Player currentPlayer;

if (ptrToShot!=NULL)
{
	if(ptrToShot->getMade())
	currentPoints+=(*ptrToShot).getPoints();
	
	int shooterNum=(*ptrToShot).getPlayerNumber();
	vector<Player>::iterator it;

	for (it=players.begin() ; it < players.end(); it++)
	{
	currentPlayer=*it;
		if (currentPlayer.getPlayerNumber()==shooterNum)
		{
		(*it).addShot(ptrToShot);
		}
	}
}

}


void Team::addTeamEvent(Event *someEvent)
{
if (someEvent!=NULL)
{
	//Add event to the teams list of events
	teamEvents.push_back(someEvent);
	int commitPlayerNum=(*someEvent).getPlayerNumber();
	Player commitingPlayer(commitPlayerNum) ;
	//Player commitingPlayer(5);
	//Add event to the players list of events
	vector<Player>::iterator it;
	it = find (players.begin(),players.end(), commitingPlayer);
	(*it).addPlayerEvent(someEvent);

	//If it's a shot, refer to the more specific addPoints function. Some preliminary analysis of shots is done as they come in, as oppsed to calculations for less important events which are only done as they are needed
	if ((someEvent->getType())=="SH")
		{
		Shot * someShot;
		someShot=dynamic_cast<Shot*> (someEvent);
		addPoints(someShot);
		}
}

}

int Team::getFouls(int PlayerNumber)  //PlayerNumber=999 to get all player fouls
{
int teamFouls=0;

	for (int i=0; i<teamEvents.size();i++)
	{
		if ((*teamEvents[i]).getType()=="F")
		{
		teamFouls++;
		}
	}
return teamFouls;

}


int Team::getBonus(int period)
{
int foulsThisPer=0;

	for (int i=0; i<teamEvents.size();i++)
	{
		if (teamEvents[i]->getType()=="F") 
		{
			if ((teamEvents[i]->getTime()).getPeriod()==period)
			foulsThisPer++;
		}
	}
if (foulsThisPer>=10)
	return 2;		//double bonus status
else if (foulsThisPer>=7)
	return 1;		//single bonus status
else
	return 0;		//no applicable bonus

}




 
int Team::getViolations()
{
int teamVios=0;

	for (int i=0; i<teamEvents.size();i++)
	{
		if ((*teamEvents[i]).getType()=="V")
		{
		teamVios++;
		}
	}
return teamVios;

}


double Team::getShotPercent(int selectPoints)
{
int totalAttempts=0;
int totalSuccess=0;
double percent=0;

	for (int i=0; i<players.size();i++)
	{
	totalAttempts+=players[i].getShotAttempts(selectPoints);	
	totalSuccess+=players[i].getShotSuccess(selectPoints);
	}

if (totalAttempts > 0) 
{
percent=(static_cast<double>(totalSuccess))/(static_cast<double>(totalAttempts));

}
else
{
percent=0;

}


return percent;

}

void Team::displayPointsByPer(int per)
{

int counter=0;
for (int i=0; i < players.size(); i++)
	{
	counter+=players[i].getPointsByPer(per);
	}
cout<<m_name<<":";	
cout<<counter<<" ";
}

Time Team::getPossessionStartTo(Time timeOfLastEvent)
{
int maxPeriod;
maxPeriod=timeOfLastEvent.getPeriod();

if (maxPeriod < 4)
{
maxPeriod=4;
}
//cout<<"max period = "<<maxPeriod<<endl;
Time gameEnds(maxPeriod,11,60);
//if this team ended with the ball, add to the possession time the remaining time between the last event and the endof game

Time difference=gameEnds-m_prevTime;


	if(m_hasBall)
	{
	m_posTime=m_posTime+difference;
	

	}


return m_posTime; 

}

bool Team::addTime(Event *ptrToEvent)
{
Time eventTime=(*ptrToEvent).getTime();

Time difference=eventTime-m_prevTime;
	if (m_hasBall)
	{
	m_posTime=m_posTime+difference;

	}

	//Set the possession for the Team from this point on, depending on what event occurred
	//the opposite possesion will be applied to the other team
	
	if (ptrToEvent->getTeam()==m_name[0])
	{
		if (ptrToEvent->getType()=="F")
		{	
		m_hasBall=false;
		}
		else if (ptrToEvent->getType()=="SH")
		{
			if((dynamic_cast<Shot*> (ptrToEvent))->getMade())
			{	
			m_hasBall=false;	//if a shot is made, possesion changes
			}
			else
			m_hasBall=true;
		}
		else if (ptrToEvent->getType()=="RB")
		{
		m_hasBall=true;
		}	
		else if (ptrToEvent->getType()=="V")
		{
		m_hasBall=false;
		}
		else if (ptrToEvent->getType()=="JB")
		{
		m_hasBall=true;
		}
		else if (ptrToEvent->getType()=="ST")
		{
		m_hasBall=true;
		}	
		else
		cout <<"Unrecognized event"<<endl;
	}
m_prevTime=eventTime;

return m_hasBall;
}



void Team::showLiveStats()
{

//- For each player on each team, report:  name, number, fouls, rebounds, steals, 2-pointers attempted, 2-pointers made, 2-pointer percentage made, same for 3-pointers, and the same for free throws. 
//cout NAME: Number: Fouls: Revbounds: Steals: get 2pt attempt :get 2pt made : divde those : wash, rinse repeat
int printWidth=12;

cout<<"Team: "<<m_name<<endl;
cout.setf(ios::left);
cout.width(printWidth);
cout<<"Plyr Name";
cout.width(printWidth);
cout<<"Plyr $";
cout.width(printWidth);
cout<<"Fouls";
cout.width(printWidth);
cout<<"Rebounds";
cout.width(printWidth);
cout<<"Steals";
cout.width(printWidth);
cout<<"1 pt ampt ";
cout.width(printWidth);
cout<<"1 pt made ";
cout.width(printWidth);
cout<<"1 pt % ";
cout.width(printWidth);
cout<<"2 pt ampt ";
cout.width(printWidth);
cout<<"2 pt made ";
cout.width(printWidth);
cout<<"2 pt % ";
cout.width(printWidth);
cout<<"3 pt ampt ";
cout.width(printWidth);
cout<<"3 pt made ";
cout.width(printWidth);
cout<<"3 pt % ";
cout<<endl;

vector<Player>::iterator it;
	for (int i=0; i<players.size(); i++)
	{
	cout.setf(ios::left);
	cout.width(printWidth);
	cout<<players[i].getPlayerName();
	cout.width(printWidth);
	cout<<players[i].getPlayerNumber();
	cout.width(printWidth);
	cout<<players[i].countPlayerEvents("F");
	cout.width(printWidth);
	cout<<players[i].countPlayerEvents("RB");
	cout.width(printWidth);
	cout<<players[i].countPlayerEvents("ST");
	cout.width(printWidth);


		for (int shotValue=1;shotValue<4;shotValue++)
		{
		
			cout.width(printWidth);
			cout<<players[i].getShotAttempts(shotValue);
			cout.width(printWidth);
			cout<<players[i].getShotSuccess(shotValue);
			cout.width(printWidth);
			cout<<players[i].getShotPercent(shotValue);
		}

	cout<<endl;
	cout<<"----------------------------------------------------------------------------------------------------------------------------------------"<<endl;
	}

}


string Team::checkUpcomingFTShooter()
{
//if last event is a free through
Event* lastEvent=teamEvents.back();
string eventType=lastEvent->getType();
Foul* lastFoul=NULL;
Player playerFouled;

	if (eventType=="F")
	{
	lastFoul=(dynamic_cast<Foul*> (lastEvent));	
	playerFouled=(*lastFoul).getPlayerFouled();
	

	}

}










